﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using MODI;

namespace WindowsTest
{
    public class OCR
    {
        [DllImport("RsLpImage.dll", EntryPoint = "BPOJPGToTiff", CharSet = CharSet.Ansi)]
        private static extern bool BPOJPGToTiff([MarshalAs(UnmanagedType.LPStr)]string oldPath, [MarshalAs(UnmanagedType.LPStr)]string newPath);

        public static bool ImageToOCR(string path, ref string value)
        {
            bool flag = true;
            var _MODIDocument = new Document();
            try
            {
                _MODIDocument.Create(path);

                _MODIDocument.OCR(MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, false, false);
                var mage = _MODIDocument.Images[0] as Image;
                if (mage != null)
                    value = mage.Layout.Text;
                _MODIDocument.Save();
            }
            catch (Exception e)
            {
                flag = false;
                LogService.Error("OCR does't work,the path:" + path, e);
            }
            finally
            {
                _MODIDocument = null;
            }

            return flag;
        }
    }
}
